'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, DECIMAL, DATE } = app.Sequelize;

  const Product = app.model.define('product', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    name: { type: STRING(100), allowNull: false, comment: '商品名称' },
    description: { type: TEXT, comment: '商品描述' },
    price: { type: DECIMAL(10, 2), allowNull: false, comment: '价格' },
    image: { type: STRING(255), comment: '商品图片' },
    images: { type: TEXT, comment: '商品图片集，JSON格式' },
    categoryId: { type: INTEGER, allowNull: false, comment: '分类ID' },
    stock: { type: INTEGER, defaultValue: 0, comment: '每日库存' },
    status: { type: INTEGER, defaultValue: 1, comment: '状态：1-上架，0-下架' },
    createdAt: { type: DATE, defaultValue: app.Sequelize.NOW },
    updatedAt: { type: DATE, defaultValue: app.Sequelize.NOW },
  });

  Product.associate = function() {
    app.model.Product.belongsTo(app.model.Category, { foreignKey: 'categoryId', as: 'category' });
  };

  return Product;
};
